import * as TYPES from './../action-type'
const initial = {
    collapsed: false,
    topRouter:[],
    actionType:'add'
}

const siderReducer = (state = initial, action) => {
    state = {...state};

    //区分是点击左侧菜单添加， 还是点击Tag删除
    if(action.type === TYPES.SET_ACTION_TYPE) {
        state.actionType = action.actionType;
    }

    // 左侧菜单折叠和收起
    if(action.type === TYPES.SIDER_COLLAPSE_BTN) {
        state.collapsed = !state.collapsed;
    }

    // 加路由
    if(action.type === TYPES.ADD_ROUTE) {
        let key = action.route.key;
        let flag = false; //默认没有要添加的路由
        state.topRouter.forEach(route => {
            if(route.key === key) {
                flag = true;
            }
        });

        !flag && ( state.topRouter = [...state.topRouter, action.route]);
    }

    // 删路由
    if(action.type === TYPES.DELETE_ROUTE) {
        state.topRouter = state.topRouter.filter(route => {
            return route.key !== action.key;
        });
    }

    // 当前路由高亮
    if(action.type === TYPES.UPDATE_COLOR) {
        state.topRouter = state.topRouter.map(route => {
            route.tagColor = (route.key === action.key ? '#108ee9' : '');
            return route;
        });
    }

    return state;
}


export default siderReducer;